﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MarkDownFileSplitter.AIAnalaysis;
using MarkDownFileSplitter.Htm;
using MarkDownFileSplitter.Pdf;
using Microsoft.Web.WebView2.WinForms;
using Newtonsoft.Json; // 引入WebView2命名空间

namespace MarkDownFileSplitter
{
    public partial class FormHeadingMarker : Form
    {
        private CatalogTreeHandler m_catalogTreeHandler;
        
        //htm那边的方法都是异步，所以不太好提取一个基类，暂时先各自使用
        private HtmHeadingMarker m_htmHeadingMarker;
        private PdfHeadingMarker m_pdfHeaingMarker;

        private InputFileType m_inputFileType;

        private string m_filePath;

        public FormHeadingMarker(string filePath, InputFileType inputFileType)
        {
            InitializeComponent();
            /*Name = "webView2";
            Size = new Size(800, 450);
            TabIndex = 0;*/
            this.WindowState = FormWindowState.Maximized;

            m_inputFileType = inputFileType;
            m_catalogTreeHandler = new CatalogTreeHandler(this.treeView_catalog.Nodes);
            m_filePath = filePath;

            //用于标记是否已保存md文件
            this.DialogResult = DialogResult.Cancel;

            // 创建一个工具栏面板
            Panel toolPanel = new Panel();
            toolPanel.Dock = DockStyle.Top;
            toolPanel.Height = 40;
            toolPanel.BackColor = Color.LightGray;
            // 创建标题1按钮
            Button btnHeading1 = new Button();
            btnHeading1.Text = "标题1";
            btnHeading1.Width = 80;
            btnHeading1.Location = new Point(10, 8);
            btnHeading1.Click += (s, args) => ApplyHeadingStyle(1);
            // 创建标题2按钮
            Button btnHeading2 = new Button();
            btnHeading2.Text = "标题2";
            btnHeading2.Width = 80;
            btnHeading2.Location = new Point(100, 8);
            btnHeading2.Click += (s, args) => ApplyHeadingStyle(2);
            // 创建标题3按钮
            Button btnHeading3 = new Button();
            btnHeading3.Text = "标题3";
            btnHeading3.Width = 80;
            btnHeading3.Location = new Point(190, 8);
            btnHeading3.Click += (s, args) => ApplyHeadingStyle(3);
            // 创建正文按钮
            Button btnNormal = new Button();
            btnNormal.Text = "正文";
            btnNormal.Width = 80;
            btnNormal.Location = new Point(280, 8);
            btnNormal.Click += (s, args) => ApplyHeadingStyle(0);
            // 创建转换为Markdown按钮
            Button btnToMarkdown = new Button();
            btnToMarkdown.Text = "转为Markdown";
            btnToMarkdown.Width = 120;
            btnToMarkdown.Location = new Point(370, 8);
            btnToMarkdown.Click += async (s, args) => SaveAsMarkdown();
            // 将按钮添加到工具栏面板
            toolPanel.Controls.Add(btnHeading1);
            toolPanel.Controls.Add(btnHeading2);
            toolPanel.Controls.Add(btnHeading3);
            toolPanel.Controls.Add(btnNormal);
            toolPanel.Controls.Add(btnToMarkdown);
            // 将工具栏面板添加到窗体
            this.Controls.Add(toolPanel);

            //需要拆成两种情况：
            //htm
            //1.初始化WebView
            //2.加载htm文本
            //3.修改标题样式的方法

            //pdf提取得到的纯文字
            //1.初始化richText
            //2.加载得到的文字
            //3.修改标题样式的方法
            //还要加一个转换为Markdown的方法（最后从TreeView的节点Tag中获取文字的起始和终点位置，添加Markdown标题符号，但前提是不允许修改文字）
        }

        private async void FormHtmMarker_Load(object sender, EventArgs e)
        {
            if (m_inputFileType == InputFileType.Htm)
            {
                m_htmHeadingMarker = new HtmHeadingMarker(m_catalogTreeHandler);
                m_htmHeadingMarker.InitWidget(this.tableLayoutPanel1.Controls);
                await m_htmHeadingMarker.ReadText(m_filePath);
            }
            else if (m_inputFileType == InputFileType.Pdf)
            {
                m_pdfHeaingMarker = new PdfHeadingMarker(m_catalogTreeHandler);
                m_pdfHeaingMarker.InitWidget(this.tableLayoutPanel1.Controls);
                m_pdfHeaingMarker.ReadText(m_filePath);
            }
        }

        /// <summary>
        /// 将选中的文本应用标题样式
        /// </summary>
        /// <param name="headingLevel">标题级别（1-3）</param>
        private void ApplyHeadingStyle(int headingLevel)
        {
            if (m_inputFileType == InputFileType.Htm)
            {
                m_htmHeadingMarker.ApplyHeadingStyle(headingLevel);
            }
            else if (m_inputFileType == InputFileType.Pdf)
            {
                m_pdfHeaingMarker.ApplyHeadingStyle(headingLevel);
            }
        }

        private async Task SaveAsMarkdown()
        {
            if (m_inputFileType == InputFileType.Htm)
            {
                await m_htmHeadingMarker.ConvertToMarkdown();
            }
            else if (m_inputFileType == InputFileType.Pdf)
            {
                m_pdfHeaingMarker.ConvertToMarkdown();
            }

            //表示已保存md文件
            this.DialogResult = DialogResult.OK;
        }
    }
}
